import {
	HOST
} from '@/config';
export const qs = ({
	url = '',
	...other
} = {}) => {
	let res = []
	for (let key in other) {
		let value = other[key]
		if (typeof value === 'object') {
			value = JSON.stringify(value)
		}
		res.push(`${key}=${value}`)
	}
	res = res.join('&')
	if (url) {
		res = {
			url: `${url}?${res}`
		}
	}
	return res
};

export const uploadFile = (name, cb, data, params, count) => {
	var that = this;
	var count = count ? count : 1;
	var chooseParams = {
		count: count,
		success: (res1) => {
			typeof cb == "function" && cb(1, name, res1);
			var tempFilePaths = res1.tempFilePaths;
			for (var tempFileIndex in tempFilePaths) {
				uni.uploadFile({
					url: HOST + '/api/file/upload',
					// #ifdef MP-ALIPAY
					fileType: 'image',
					// #endif
					filePath: tempFilePaths[tempFileIndex],
					name: 'file',
					header: {
						'content-type': 'application/x-www-form-urlencoded'
					},
					formData: data || {},
					success: function(res3) {
						console.log(res3);
						res3 = JSON.parse(res3.data.trim());
						if (res3.code == 200) {
							typeof cb == "function" && cb(2, name, res3.data);
						} else {
							uni.showToast({
								icon: 'none',
								title: res3.msg
							});
						}
					},
					fail: (res2) => {
						typeof cb == "function" && cb(-2, name, res2);
					}
				})
			}
		},
		fail(res1) {
			typeof cb == "function" && cb(-1, name, res1);
		}
	};
	if (typeof params == 'object') {
		for (var i in params) chooseParams[i] = params[i];
	}
	uni.chooseImage(chooseParams)
};

export const adornUrl = (url) => {
	if (url) return url.startsWith('http') ? url : HOST + url;
	else return url;
};

export const getUrlParam = (name, url) => {
	let param = url.substr(url.indexOf('?') + 1);
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
	var r = param.match(reg);
	if (r != null) return unescape(r[2]);
	return '';
};

export const getCurrentPath = () => {
	return getCurrentPages()[getCurrentPages().length - 1].route;
};

export const getRoute = (index) => { //获取历史路由对象,默认当前
	let page = index ? parseInt(index) : 1;
	let pages = getCurrentPages();
	let prevPage = pages[pages.length - page];
	if (prevPage == undefined || prevPage == null) return 0;
	return prevPage;
};

export const formatTime = (time, ff = 'Y-m-d H:i:s') => {
	if (!time) return time;
	if (Number(time).toString().length == 10) {
		time = Number(time + "000")
	}
	time = new Date(time);
	let year = time.getFullYear();
	let month =
		time.getMonth() + 1 < 10 ?
		"0" + (time.getMonth() + 1) :
		time.getMonth() + 1;
	let date = time.getDate() < 10 ? "0" + time.getDate() : time.getDate();
	let hour = time.getHours() < 10 ? "0" + time.getHours() : time.getHours();
	let minute =
		time.getMinutes() < 10 ? "0" + time.getMinutes() : time.getMinutes();
	let second =
		time.getSeconds() < 10 ? "0" + time.getSeconds() : time.getSeconds();
	if (ff == "Y-m-d") {
		return year + "-" + month + "-" + date;
	} else if (ff == "Y-m-d H:i:s") {
		return (
			year +
			"-" +
			month +
			"-" +
			date +
			" " +
			hour +
			":" +
			minute +
			":" +
			second
		);
	} else if (ff == "Y-m-d H:i") {
		return year + "-" + month + "-" + date + " " + hour + ":" + minute;
	} else if (ff == "Y.m.d") {
		return year + "." + month + "." + date;
	} else if (ff == "Y.m.d H:i:s") {
		return (
			year +
			"." +
			month +
			"." +
			date +
			" " +
			hour +
			":" +
			minute +
			":" +
			second
		);
	} else if (ff == "Y.m.d H:i") {
		return year + "." + month + "." + date + " " + hour + ":" + minute;
	}
};

export const formatImg = (url) => {
	if (url) return url.startsWith('http') ? url : HOST  +'/'+ url;
}
